package org.payeco.risk.controller;

import java.util.List;

import org.payeco.risk.dao.PayecoRiskRuleRepository;
import org.payeco.risk.entity.PayecoRiskRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yps
 *
 */
@RestController
public class PayecoRuleController {
    @Autowired
    PayecoRiskRuleRepository ruleRepository;

    @RequestMapping("/saveRule")
    public PayecoRiskRule save(String classname, String checkmode,
			String state, Integer priority, String pattern, String riskaction,
			String riskno) {
    	PayecoRiskRule rule = ruleRepository.save(new PayecoRiskRule(null, classname, checkmode, state, priority, pattern, riskaction, riskno));
        return rule;
    }

    @RequestMapping("/queryRules")
    public List<PayecoRiskRule> queryRules(String state) {
        List<PayecoRiskRule> riskRules = ruleRepository.findByState(state);
        return riskRules;
    }

    @RequestMapping("/queryByMerNo")
    public PayecoRiskRule q2(String state, String sysMerNo) {
    	PayecoRiskRule riskRule = ruleRepository.findByStateAndSysmerchantno(state, sysMerNo).get(0);
        return riskRule;
    }

    @RequestMapping("/queryByRiskNo")
    public PayecoRiskRule q3(String riskno) {
    	PayecoRiskRule riskRule = ruleRepository.findByRiskNo(riskno);
        return riskRule;
    }

    @RequestMapping("/sort")
    public List<PayecoRiskRule> sort() {
        List<PayecoRiskRule> riskRules = ruleRepository.findAll(new Sort(Sort.Direction.ASC, "priority"));
        return riskRules;
    }
    @RequestMapping("/page")
    public Page<PayecoRiskRule> page(int page,int size){
        Page<PayecoRiskRule> all = ruleRepository.findAll(new PageRequest(page, size));
        return all;
    }
    @RequestMapping("/all")
    public List<PayecoRiskRule> all(){
        return ruleRepository.findAll();
    }
}
